<?php

use yii\helpers\Url;


/* @var $this yii\web\View */
/* @var $model app\models\Recall */
/* @var $form yii\widgets\ActiveForm */
?>
<style type="text/css">
    #select_order_datetime_button td .layui-btn-xs {
        width: 50px;
        height: 28px;
        line-height: 28px;
        display: inline-block;
        border: #999 1px solid;
        border-radius: 5px;
        padding: 0 5px;
        text-align: center;
        cursor: pointer;
    }
</style>

<div class="order-time-form" id="order_time_id" style="display: none;">
    <div class="layui-form layui-form-pane" style="padding:1em;" id="order_time" style="width:500px;padding:20px;">
        <div class="layui-form-item">
            <input type="hidden" name="patient_id" value="" />
        </div>
        <div class="layui-form-item" id="order_time_2_div">
            <label class="layui-form-label">预约时间</label>
            <div class="layui-input-block">
                <input id="order_time_2" type="text" name="order_time" autocomplete="off" class="layui-input" />
            </div>
            <table id="select_order_datetime_button" style="margin:0 0.5em;width:100%;">
                <tr>
                    <td style="padding:0.5em;border-bottom:#ddd 1px solid;">
                        <div onclick="select_order_date('<?= date('Y-m-d') ?>')" class="layui-btn-xs">今天</div>
                        <div onclick="select_order_date('<?= date('Y-m-d', strtotime('+1 day')) ?>')" class="layui-btn-xs">明天</div>
                        <div onclick="select_order_date('<?= date('Y-m-d', strtotime('+2 day')) ?>')" class="layui-btn-xs">后天</div>
                        <div onclick="select_order_date('<?= date('Y-m-d', strtotime('+3 day')) ?>')" class="layui-btn-xs">大后天</div>
                        <div onclick="select_order_date('<?= date('Y-m-d', strtotime('next Saturday')) ?>')" class="layui-btn-xs">周六</div>
                        <div onclick="select_order_date('<?= date('Y-m-d', strtotime('next Sunday')) ?>')" class="layui-btn-xs">周日</div>
                        <div onclick="select_order_date('<?= date('Y-m-d', strtotime('next Monday')) ?>')" class="layui-btn-xs">周一</div>
                        <div onclick="select_order_date('<?= date('Y-m-d', strtotime('+7 day')) ?>')" class="layui-btn-xs">一周后</div>
                        <div onclick="select_order_date('<?= date('Y-m-d', strtotime('+15 day')) ?>')" class="layui-btn-xs">半月后</div>
                    </td>
                </tr>
                <tr>
                    <td style="padding:0.5em;">
                        <div onclick="select_order_time('08:00:00')" class="layui-btn-xs">8点</div>
                        <div onclick="select_order_time('09:00:00')" class="layui-btn-xs">9点</div>
                        <div onclick="select_order_time('10:00:00')" class="layui-btn-xs">10点</div>
                        <div onclick="select_order_time('11:00:00')" class="layui-btn-xs">11点</div>
                        <div onclick="select_order_time('12:00:00')" class="layui-btn-xs">12点</div>
                        <div onclick="select_order_time('13:00:00')" class="layui-btn-xs">13点</div>
                        <div onclick="select_order_time('14:00:00')" class="layui-btn-xs">14点</div>
                        <div onclick="select_order_time('15:00:00')" class="layui-btn-xs">15点</div>
                        <div onclick="select_order_time('16:00:00')" class="layui-btn-xs">16点</div>
                        <div onclick="select_order_time('17:00:00')" class="layui-btn-xs">17点</div>
                        <div onclick="select_order_time('18:00:00')" class="layui-btn-xs">18点</div>
                    </td>
                </tr>
            </table>
        </div>
        <div class="layui-form-item button" style="position: absolute;bottom:5px;right:20px;">
            <button class="layui-btn" onclick="patient_order_time_ajax()" style="width: 100%;">
                保 存
            </button>
        </div>
    </div>
</div>

<script type="text/javascript">
    window.setTimeout("init_order_time_layer()", 500);

    function init_order_time_layer() {
        window.order_time_content_html = $('#order_time_id').html();
        $('#order_time_id').remove();
    }

    function patient_order_time(patient_name, id) {
        layui.layer.open({
            type: 1,
            title: '设置患者预约时间: ' + patient_name,
            area: ['650px', '440px'],
            content: window.order_time_content_html,
        });
        window.yyh_selected_patient_id = id;
        return layui.laydate.render({
            min: '<?= date('Y-m-d') ?>',
            elem: '#order_time_2',
            type: 'datetime',
            isInitValue: true
        });
    }

    window.patient_order_time_ajax = function() {
        var form_data = {};
        form_data._csrf = '<?= Yii::$app->request->csrfToken ?>';
        form_data['patient[order_time]'] = $('#order_time_2').val();
        $.ajax({
            url: "<?= Url::toRoute('patient/recordtoorder') ?>&id=" + window.yyh_selected_patient_id,
            data: form_data,
            dataType: 'json',
            type: 'post',
            async: false,
            success: function(res) {
                if (res.status == 'success') {
                    layui.layer.msg('成功添加了预约时间');
                    return window.location.reload();
                } else {
                    layui.layer.msg('失败');
                }

            }
        });
    };

    function select_order_date(value_date) {
        $('#order_time_2').val(value_date + ' 00:00:00');
    }

    function select_order_time(value_time) {
        var selected_date = $('#order_time_2').val();
        if (selected_date.length < 10) {
            layer.alert('先选择日期，才能选择时间', {
                icon: 2
            });
            return false;
        }
        $('#order_time_2').val(selected_date.slice(0, 10) + ' ' + value_time);
    }
</script>